package VIEWS;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GraphicsConfiguration;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;

import CONTROLLERS.HoaDonControllers;
import CONTROLLERS.UsersControllers;
import ModelEntityFW.HoaDonEntitys;
import ModelEntityFW.UserEntitys;
import Ultils.DateConverter;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;
import com.toedter.calendar.JDateChooser;
import javax.swing.ImageIcon;

public class frmThemHoaDon extends JFrame {

	private JPanel contentPane;
	private JTextField txtCTBAID;
	private JTextField txtBHYT;
	private JTextField txtGTGT;
	private JTextField txtTongtien;
	private JTextField txtHoadonID;
	private JTextField txtTienthanhtoan;
	private final JComboBox cboLoaiBH;
	private JDateChooser txtNgaytaoHD;

	private static Connection m_objConnection;
	private static UserEntitys m_objSecurityContext;
	private static HoaDonEntitys m_objHoaDonEntity;
	private HoaDonControllers m_objHoaDonController;

	/**
	 * Launch the application.
	 */

	/**
	 * Create the frame.
	 */
	public frmThemHoaDon(Connection objConnection, UserEntitys objSecurity, HoaDonEntitys objHoaDonEntity) {
		setTitle("Thêm hóa đơn");
		// Khởi tạo đối tượng
		m_objConnection = objConnection;
		m_objSecurityContext = objSecurity;
		m_objHoaDonEntity = objHoaDonEntity;
		m_objHoaDonController = new HoaDonControllers(m_objConnection, m_objSecurityContext);

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 503, 347);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("Hóa đơn ID:");
		lblNewLabel.setBounds(10, 11, 97, 14);
		contentPane.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("Ngày tạo hóa đơn:");
		lblNewLabel_1.setBounds(10, 46, 108, 14);
		contentPane.add(lblNewLabel_1);

		JLabel lblNewLabel_2 = new JLabel("Tổng tiền:");
		lblNewLabel_2.setBounds(10, 82, 97, 14);
		contentPane.add(lblNewLabel_2);

		JLabel lblGtgt = new JLabel("GTGT:");
		lblGtgt.setBounds(10, 121, 46, 14);
		contentPane.add(lblGtgt);

		JLabel lblBhyt = new JLabel("BHYT:");
		lblBhyt.setBounds(10, 159, 46, 14);
		contentPane.add(lblBhyt);

		JLabel lblSTinThanh = new JLabel("Số tiền thanh toán:");
		lblSTinThanh.setBounds(10, 199, 108, 14);
		contentPane.add(lblSTinThanh);

		JLabel lblCtbaid = new JLabel("CTBA-ID:");
		lblCtbaid.setBounds(248, 121, 84, 14);
		contentPane.add(lblCtbaid);

		JLabel lblLoiBoHim = new JLabel("Loại bảo hiểm:");
		lblLoiBoHim.setBounds(248, 159, 84, 14);
		contentPane.add(lblLoiBoHim);

		cboLoaiBH = new JComboBox();
		cboLoaiBH.setBounds(342, 156, 108, 20);
		cboLoaiBH.setModel(new DefaultComboBoxModel<>(new String[] { "Thường", "VIP" }));
		contentPane.add(cboLoaiBH);

		txtCTBAID = new JTextField();
		txtCTBAID.setBounds(342, 118, 108, 20);
		contentPane.add(txtCTBAID);
		txtCTBAID.setColumns(10);

		txtBHYT = new JTextField();
		txtBHYT.setBounds(117, 156, 102, 20);
		contentPane.add(txtBHYT);
		txtBHYT.setColumns(10);

		txtGTGT = new JTextField();
		txtGTGT.setColumns(10);
		txtGTGT.setBounds(117, 118, 102, 20);
		contentPane.add(txtGTGT);

		txtTongtien = new JTextField();
		txtTongtien.setColumns(10);
		txtTongtien.setBounds(117, 79, 333, 20);
		contentPane.add(txtTongtien);

		txtHoadonID = new JTextField();
		txtHoadonID.setColumns(10);
		txtHoadonID.setBounds(117, 8, 333, 20);
		contentPane.add(txtHoadonID);

		txtTienthanhtoan = new JTextField();
		txtTienthanhtoan.setEditable(false);
		txtTienthanhtoan.setBounds(117, 196, 333, 55);
		contentPane.add(txtTienthanhtoan);
		txtTienthanhtoan.setColumns(10);

		JButton btnNewButton = new JButton("Thêm hóa đơn");
		btnNewButton.setIcon(new ImageIcon(frmThemHoaDon.class.getResource("/images/book_edit.png")));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// Thiết lập thông tin cho Entity
				m_objHoaDonEntity.setNgayTaoHD(DateConverter.convertDateToString(txtNgaytaoHD.getDate(), "dd/MM/yyyy"));
				m_objHoaDonEntity.setTongTien(Float.parseFloat(txtTongtien.getText()));
				txtGTGT.setText((Float.parseFloat(txtTongtien.getText())/10)+"");
				m_objHoaDonEntity.setGTGT(Float.parseFloat(txtGTGT.getText()));
				if(cboLoaiBH.getSelectedIndex() == 0){
					txtBHYT.setText("50000");
				}else{
					txtBHYT.setText("80000");
				}
				m_objHoaDonEntity.setBHYT(Float.parseFloat(txtBHYT.getText()));
				txtTienthanhtoan.setText((Float.parseFloat(txtTongtien.getText()) + Float.parseFloat(txtGTGT.getText()) - Float.parseFloat(txtBHYT.getText()))+"");
				m_objHoaDonEntity.setSoTienThanhToan(Float.parseFloat(txtTienthanhtoan.getText()));
				m_objHoaDonEntity.setCTBA_ID(Integer.parseInt(txtCTBAID.getText()));
				m_objHoaDonEntity.setLoaiBH("" + cboLoaiBH.getSelectedItem());
				
				
				
				// 4. Truy�?n thông tin cho UserController xử lý
				if (m_objHoaDonController == null) {
					m_objHoaDonController = new HoaDonControllers(m_objConnection, m_objSecurityContext);
				}

				// Thực thi Thêm hoặc Cập Nhật
				if (m_objHoaDonEntity.isbFlag()) {
					m_objHoaDonEntity.setHoaDonID(txtHoadonID.getText());
					boolean bResult = false;
					try {
						bResult = m_objHoaDonController.ExecOfUpdateElement(m_objHoaDonEntity);
					} catch (Exception e) {
						JOptionPane.showMessageDialog(getParent(), "Err: " + e, "Thông báo",
								JOptionPane.INFORMATION_MESSAGE);
					}
					if (bResult) {
						JOptionPane.showMessageDialog(getParent(), "Cập nhật hóa đơn thành công!", "Thông báo",
								JOptionPane.INFORMATION_MESSAGE);

						frmHoaDon fHD = new frmHoaDon(m_objConnection, m_objSecurityContext);
						fHD.setVisible(true);
						dispose();
					}
				} else {
					boolean bResult = false;
					try {
						bResult = m_objHoaDonController.ExecOfInsertElement(m_objHoaDonEntity);
					} catch (Exception e) {
						JOptionPane.showMessageDialog(getParent(), "Err: " + e, "Thông báo",
								JOptionPane.INFORMATION_MESSAGE);
					}
					if (bResult) {
						JOptionPane.showMessageDialog(getParent(), "Thêm hóa đơn thành công!", "Thông báo",
								JOptionPane.INFORMATION_MESSAGE);

						frmHoaDon fHD = new frmHoaDon(m_objConnection, m_objSecurityContext);
						fHD.setVisible(true);
						// Giải phóng form
						dispose();
					}
				}
			}
		});
		btnNewButton.setBounds(177, 262, 148, 23);
		contentPane.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("Đóng");
		btnNewButton_1.setIcon(new ImageIcon(frmThemHoaDon.class.getResource("/images/cancel.gif")));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnNewButton_1.setBounds(348, 262, 89, 23);
		contentPane.add(btnNewButton_1);

		txtNgaytaoHD = new JDateChooser();
		txtNgaytaoHD.setDateFormatString("dd/MM/yyyy");
		txtNgaytaoHD.setBounds(117, 40, 108, 20);
		contentPane.add(txtNgaytaoHD);
		if(m_objHoaDonEntity.isbFlag()){
			BindingControls(m_objHoaDonEntity);
		}
	}

	void BindingControls(HoaDonEntitys objHoaDonEntity) {
		txtHoadonID.setText(objHoaDonEntity.getHoaDonID());
		txtNgaytaoHD.setDate(DateConverter.convertStringToDate(objHoaDonEntity.getNgayTaoHD(), "yyyy-MM-dd"));
		txtTongtien.setText("" + objHoaDonEntity.getTongTien());
		txtGTGT.setText("" + objHoaDonEntity.getGTGT());
		txtBHYT.setText("" + objHoaDonEntity.getBHYT());
		txtCTBAID.setText("" + objHoaDonEntity.getCTBA_ID());
		txtTienthanhtoan.setText("" + objHoaDonEntity.getSoTienThanhToan());
        cboLoaiBH.setSelectedItem(objHoaDonEntity.getLoaiBH());  
	}
}
